home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / c / mkid.lha / src / stoi.c < prev    next >
C/C++ Source or Header  |  1991-02-01  |  2KB  |  111 lines

  1. /* Copyright (c) 1986, Greg McGary */
  2. static char sccsid[] = "@(#)stoi.c    1.1 86/10/09";
  3.  
  4. #include    "radix.h"
  5. #include    <ctype.h>
  6.  
  7. int dtoi();
  8. int otoi();
  9. int radix();
  10. int stoi();
  11. int xtoi();
  12.  
  13. /*
  14.     Use the C lexical rules to determine an ascii number's radix.
  15.     The radix is returned as a bit map, so that more than one radix
  16.     may apply.  In particular, it is impossible to determine the
  17.     radix of 0, so return all possibilities.
  18. */
  19. int
  20. radix(name)
  21.     register char    *name;
  22. {
  23.     if (!isdigit(*name))
  24.         return 0;
  25.     if (*name != '0')
  26.         return RADIX_DEC;
  27.     name++;
  28.     if (*name == 'x' || *name == 'X')
  29.         return RADIX_HEX;
  30.     while (*name && *name == '0')
  31.         name++;
  32.     return (RADIX_OCT | ((*name)?0:RADIX_DEC));
  33. }
  34.  
  35. /*
  36.     Convert an ascii string number to an integer.
  37.     Determine the radix before converting.
  38. */
  39. int
  40. stoi(name)
  41.     char        *name;
  42. {
  43.     switch (radix(name))
  44.     {
  45.     case RADIX_DEC:    return(dtoi(name));
  46.     case RADIX_OCT:    return(otoi(&name[1]));
  47.     case RADIX_HEX:    return(xtoi(&name[2]));
  48.     case RADIX_DEC|RADIX_OCT: return(0);
  49.     default:    return(-1);
  50.     }
  51. }
  52.  
  53. /*
  54.     Convert an ascii octal number to an integer.
  55. */
  56. int
  57. otoi(name)
  58.     char        *name;
  59. {
  60.     register int    n = 0;
  61.  
  62.     while (*name >= '0' && *name <= '7') {
  63.         n *= 010;
  64.         n += *name++ - '0';
  65.     }
  66.     if (*name == 'l' || *name == 'L')
  67.         name++;
  68.     return (*name ? -1 : n);
  69. }
  70.  
  71. /*
  72.     Convert an ascii decimal number to an integer.
  73. */
  74. int
  75. dtoi(name)
  76.     char        *name;
  77. {
  78.     register int    n = 0;
  79.  
  80.     while (isdigit(*name)) {
  81.         n *= 10;
  82.         n += *name++ - '0';
  83.     }
  84.     if (*name == 'l' || *name == 'L')
  85.         name++;
  86.     return (*name ? -1 : n);
  87. }
  88.  
  89. /*
  90.     Convert an ascii hex number to an integer.
  91. */
  92. int
  93. xtoi(name)
  94.     char        *name;
  95. {
  96.     register int    n = 0;
  97.  
  98.     while (isxdigit(*name)) {
  99.         n *= 0x10;
  100.         if (isdigit(*name))
  101.             n += *name++ - '0';
  102.         else if (islower(*name))
  103.             n += 0xa + *name++ - 'a';
  104.         else
  105.             n += 0xA + *name++ - 'A';
  106.     }
  107.     if (*name == 'l' || *name == 'L')
  108.         name++;
  109.     return (*name ? -1 : n);
  110. }
  111.